//
//  myrefresh.swift
//  mywei
//
//  Created by myd on 15/12/23.
//  Copyright © 2015年 myd1. All rights reserved.
//

import UIKit

class myrefresh: UIRefreshControl {
    let re = refreshviewredy1.refreshviewredy()
    let rejiao : CGFloat = -60
    var up : Bool = false
    override var frame :CGRect {
        didSet{

        if frame.origin.y > 0
        {
           return
            }
        if frame.origin.y > rejiao && !up
        {
            up = !up
             re.tip(up)
            }else if frame.origin.y <= rejiao && up
        {
            
        up = !up
          re.tip(up)
            }
        
        
        }
    }
    
   override init() {
        super.init()
        prepareUI()
    }

   required init?(coder aDecoder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
   }
func prepareUI()
{
   self.tintColor = UIColor.clearColor()
    
    addSubview(re)
    re.ff_AlignInner(type: ff_AlignType.CenterCenter, referView: self, size: re.bounds.size)
    }
    override func endRefreshing() {
        super.endRefreshing()
        re.view.hidden = false
    }
}
class refreshviewredy1 : UIView {
    
    @IBOutlet weak var image: UIImageView!
    @IBOutlet weak var  view : UIView!
    @IBOutlet weak var load : UIImageView!
    
    func  tip(isup : Bool)
    {
        UIView .animateWithDuration(0.3, animations: { () -> Void in
            self.image.transform = CGAffineTransformMakeRotation(isup == false ? CGFloat(M_PI - 0.001) : 0)
        })
    }
    
    class func refreshviewredy() ->refreshviewredy1
    {
 let xx =  NSBundle.mainBundle().loadNibNamed("refreshView", owner: nil, options: nil).last as! refreshviewredy1
//        if   xx.layer.animationForKey("myd") == nil{
//       return
//        }
        
        let an = CABasicAnimation(keyPath: "transform.rotation")
        an.duration=0.25
        an.repeatCount = MAXFLOAT
        an .toValue = 2*M_PI
        an.removedOnCompletion=false
        xx.load.layer.addAnimation(an, forKey: "myd")

        return xx
    }

}
